#include "time_info.h"
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
void CTimeInfo::set_start_time(void)
{
    gettimeofday(&start_time, NULL);
}

void CTimeInfo::get_delta_str(char * str_out)
{
    struct timeval curr_tv;
    gettimeofday(&curr_tv, NULL);

    int delta_sec = 0;
    int delta_ms = 0;
    
    if(curr_tv.tv_usec >= start_time.tv_usec)
    {
        delta_ms = (curr_tv.tv_usec - start_time.tv_usec)/1000;
        delta_sec = curr_tv.tv_sec - start_time.tv_sec;
    }
    else
    {
        delta_ms = (curr_tv.tv_usec + 1000000 - start_time.tv_usec)/1000;
        delta_sec = curr_tv.tv_sec - start_time.tv_sec - 1;
    }

    sprintf(str_out,"%d.%03d",delta_sec,delta_ms);
}

int main_test(void)
{
   CTimeInfo g_timeinfo; 
   g_timeinfo.set_start_time();

   char str_buff[128];

   int i = 0;
   for(i=0;i<30;i++)
   {
       g_timeinfo.get_delta_str(str_buff);
       printf("run time:%s\r\n",str_buff);
       
       sleep(1);
       usleep(40000);
   }
   
   return 0;
}